﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DMS.utils;

namespace DMS
{
    public partial class NewDb : Form
    {
        public NewDb()
        {
            InitializeComponent();
        }
        #region 公用代码
        //返回时的数据库连接状态
        public bool Status { get; private set; } = false;

        //选择连接数据库或文件时 改变显示的面板和窗体大小
        private void RbtnSql_CheckedChanged(object sender, EventArgs e)
        {
            if (RbtnSql.Checked == true)
            {
                panel1.Visible = true;
                panel2.Visible = false;
                Size = new Size(245, 318);
            }
            else
            {
                panel1.Visible = false;
                panel2.Visible = true;
                Size = new Size(245, 150);
            }
        }

        //检测数据库连接状态 若错误显示对话框
        private bool SqlStatus(string connStr)
        {
            int sqlStatus = DataBase.SqlTest(connStr);
            if (sqlStatus == 1)
            {
                MessageBox.Show("无法打开数据库连接, 数据库错误", "数据库连接失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            if (sqlStatus == -1)
            {
                MessageBox.Show("无法打开数据库连接, 未知错误", "数据库连接失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            Status = true;
            return true;
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            MessageBox.Show("未填写有效数据库信息，程序即将退出", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Dispose();
        }
        #endregion
        #region 连接数据库
        private void BtnSubmit_Click(object sender, EventArgs e)
        {
            string connstr;
            if (dbIP.Text == "" || dbName.Text == "" || dbUsr.Text == "" || dbPwd.Text == "")
            {
                MessageBox.Show("请确认信息填写是否完整，所有项均为必填项。", "请输入完整的数据库连接信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //默认链接字符串
            connstr = "Data Source=" + dbIP.Text + "," + dbPort.Text + ";DataBase=" + dbName.Text + ";uid=" + dbUsr.Text + ";pwd=" + dbPwd.Text;
            //如果端口为初始的1433 则不需要指定端口号
            if (dbPort.Value == 1433)
                connstr = "Data Source=" + dbIP.Text + ";DataBase=" + dbName.Text + ";uid=" + dbUsr.Text + ";pwd=" + dbPwd.Text;
            //测试数据库是否能连通
            if (SqlStatus(connstr) == true)
            {
                Utils.WriteDbConf(dbIP.Text, dbPort.Text, dbName.Text, dbUsr.Text, dbPwd.Text);
                Dispose();
            }
        }
        #endregion
        #region 连接数据库文件
        private void Browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog OFD = new OpenFileDialog
            {
                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
                Filter = "SQL数据库文件|*.mdf",
                RestoreDirectory = false
            };
            if (OFD.ShowDialog() == DialogResult.OK)
                dbFile.Text = System.IO.Path.GetFullPath(OFD.FileName);
        }

        private void BtnSubmitF_Click(object sender, EventArgs e)
        {
            string connstr;
            if (dbFile.Text == "")
            {
                MessageBox.Show("请选择数据库文件位置。", "请选择数据库文件位置", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //默认链接字符串
            connstr = @"Data Source =.\SQLEXPRESS;AttachDbFilename=" + dbFile.Text + ";Integrated Security=True;User Instance = True";
            //测试数据库是否能连通
            if (SqlStatus(connstr) == true)
            {
                Utils.WriteDbFileConf(dbFile.Text);
                Dispose();
            }
        }
        #endregion
    }
}
